﻿using UnityEngine;
using System.Collections;

public class playerRegenManager : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

		// OP regen
		if (GameManager.playerOP < GameManager.playerOPmax && ShipModuleData.lifeSupportON == true && GameManager.playerHP >= GameManager.lowHPth) {
			GameManager.playerOPt -= Time.deltaTime;
			if (GameManager.playerOPt <= 0f) {
				GameManager.playerOP = GameManager.playerOP + ShipModuleData.lifeSupportLv;
				GameManager.playerOPt = GameManager.defaultOPt;
			}
		}
		
		// OP loss
		if (ShipModuleData.lifeSupportON == false || GameManager.playerHP < GameManager.lowHPth) {
			GameManager.playerOPt -= Time.deltaTime;
			if (GameManager.playerOPt <= 0f)
			{
				GameManager.playerOP = GameManager.playerOP - 1;
				if (ShipModuleData.lifeSupportON == false && GameManager.playerHP < GameManager.playerHPmax/3)
					GameManager.playerOP = GameManager.playerOP -1;
				if (GameManager.playerOP <= 0 && GameManager.playerHP < GameManager.playerHPmax/3)
					GameManager.playerOP = 0;
				GameManager.playerOPt = GameManager.defaultOPt;
			}
			
			
		}
		
		//crew loss
		if (GameManager.playerOP <= GameManager.playerOPcritical && GameManager.playerCrew >= 1)
		{
			GameManager.playerCrewt -= Time.deltaTime;
			if (GameManager.playerCrewt <= 0f) {
				GameManager.playerCrew = GameManager.playerCrew - 1;
				if (GameManager.playerCrew <= 0)
					GameManager.playerHP = GameManager.playerHP - 1;
				GameManager.playerCrewt = GameManager.defaultCrewt;
			}
		}
		// shields off drain
		if (ShipModuleData.shieldGeneratorON == false && GameManager.playerSP > 0)
		{
			GameManager.playerSPt -= Time.deltaTime;
			if (GameManager.playerSPt <= 0f) {
				GameManager.playerSP = GameManager.playerSP - 3;
				GameManager.playerSPt = GameManager.defaultSPt;
			}
		}
		
		// SP regen
		if (GameManager.playerSP < GameManager.playerSPmax && ShipModuleData.shieldGeneratorON == true) // jos kilvet eivät ole täynnä ja generaattori on päällä
		{
			GameManager.playerSPt -= Time.deltaTime;
			if (GameManager.playerSPt <= 0f) {
				GameManager.playerSP = GameManager.playerSP + (ShipModuleData.shieldGeneratorLv * 3);
				GameManager.playerSPt = GameManager.defaultSPt;
			}
		}
		if (GameManager.playerSP < 0 && ShipModuleData.shieldGeneratorON == false) // jos kilvet ovat miinuksella ja generaattori on poies päältä
		{
			GameManager.playerSPt -= Time.deltaTime;
			if (GameManager.playerSPt <= 0f) {
				GameManager.playerSP = GameManager.playerSP + 1;
				GameManager.playerSPt = GameManager.defaultSPt;
			}
		}
		
		// HP regen
		if (GameManager.playerHP < GameManager.playerHPmax && ShipModuleData.repairModON == true) {
			GameManager.playerHPt -= Time.deltaTime;
			if (GameManager.playerHPt <= 0f) {
				GameManager.playerHP = GameManager.playerHP + ShipModuleData.repairModLv;
				GameManager.playerHPt = GameManager.defaultHPt;
			}
		}

	}
}
